﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MutilPanel
{
    /// <summary>
    /// ChildPanel.xaml 的交互逻辑
    /// </summary>
    public partial class ChildPanel : UserControl
    {
        private readonly MainWindow _mainWindow;
        public int _controlType = 0;
        private bool _popupIsOpen = false;
        public ChildPanel(int type)
        {
            this._controlType = type;
            InitializeComponent();
            _mainWindow = Application.Current.MainWindow as MainWindow;
            LoadContent(this._controlType); // 示例：默认加载第一个面板
        }

        private void LoadContent(int type)
        {
            switch (type)
            {
                case 0:
                    btn_Content.Content = "裸罐工位";

                    break;
                case 1:
                    btn_Content.Content = "瓶箱工位";

                    break;
                case 2:
                    btn_Content.Content = "复检工位";
                    // 清空现有的内容
                    contentGrid.Children.Clear();
                    // 加载第一个面板的内容
                    var panel1 = new ConfirmControl();
                    contentGrid.Children.Add(panel1);
                    break;
                case 3:
                    btn_Content.Content = "定位工位";
                    // 清空现有的内容
                    contentGrid.Children.Clear();
                    // 加载第一个面板的内容
                    var panel2 = new PalletControl();
                    contentGrid.Children.Add(panel2);
                    break;
            }
        }

        private void Button_Popup_Click(object sender, RoutedEventArgs e)
        {
            if (!_popupIsOpen)
            {
                // 从主窗口中移除用户控件
                if (_mainWindow != null && _mainWindow.mainGrid.Children.Contains(this))
                {
                    _mainWindow.RemoveUserControlFromMainWindow(this);
                }

                var window = new SeparateWindow(this);
                window.Closed += (s, args) =>
                {
                    // 当 PopupWindow 关闭时，将子面板重新添加到主窗口
                    if (_mainWindow != null)
                    {
                        _mainWindow.AddUserControlToMainWindow(this);
                        _popupIsOpen = false;
                    }
                };
                window.Show();
                _popupIsOpen = true;
            }
        }
    }
}
